package com.netbetter.langzheng.plugin.fileManager.widget.menu;

import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;
import com.netbetter.langzheng.plugin.fileManager.contents.CommandEnums;
import com.netbetter.langzheng.plugin.fileManager.context.ChangeListItemEventContext;
import com.netbetter.langzheng.plugin.fileManager.model.FileModel;
import com.netbetter.langzheng.plugin.fileManager.widget.commandhandle.CommandHandle;
import com.netbetter.langzheng.plugin.fileManager.widget.property.FolderPropertyDialog;

/**
 * @author 嘉陵江里捉鱼 E-mail:tang7259@163.com
 * @Date 创建时间：2008-8-29 下午11:16:07 类说明
 */
public class FolderMenu extends Menu {

    private MenuItem             propertyMenu;
    private FolderPropertyDialog propertyDialog;
    private CommandHandle        modifyHandleFile;

    public FolderMenu() {
        // 暂时不支持目录的剪贴与拷贝
        // MenuItem menuItem = new MenuItem("剪切");
        // menuItem.setIconStyle("user-add");
        // menuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
        //
        // public void componentSelected(MenuEvent ce) {
        // CopyFileEventContext.attach(ChangeListItemEventContext.getFileModel());
        // }
        // });
        // add(menuItem);
        // menuItem = new MenuItem("复制");
        // menuItem.setIconStyle("icon-table");
        // menuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
        //
        // public void componentSelected(MenuEvent ce) {
        // CutFileEventContext.attach(ChangeListItemEventContext.getFileModel());
        // }
        // });
        // add(menuItem);
        modifyHandleFile = new CommandHandle() {

            @Override
            public CommandEnums getCommandType() {
                return CommandEnums.FOLDER_MODIFY;
            }

            @Override
            public boolean isNormale() {
                return true;
            }
        };
        MenuItem menuItem = new MenuItem("重命名");
        menuItem.setIconStyle("icon-accordion");
        menuItem.addSelectionListener(new SelectionListener<MenuEvent>() {

            public void componentSelected(MenuEvent ce) {
                modifyFolderEvent();
            }
        });
        add(menuItem);
        // menuItem = new MenuItem("删除");
        // menuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
        //
        // public void componentSelected(MenuEvent ce) {
        // deleteFolderEvent();
        // }
        // });
        // menuItem.setIconStyle("icon-delete");
        // add(menuItem);
        add(new SeparatorMenuItem());
        menuItem = new MenuItem("共享");
        menuItem.setEnabled(false);
        add(menuItem);
        propertyMenu = new MenuItem();
        propertyMenu.setText("属性");
        propertyMenu.setIconStyle("icon-email");
        propertyMenu.addSelectionListener(new SelectionListener<MenuEvent>() {

            public void componentSelected(MenuEvent ce) {
                propertyDialog.show();
            }
        });
        this.add(propertyMenu);

        propertyDialog = new FolderPropertyDialog();
    }

    protected void modifyFolderEvent() {
        final FileModel modifyModel = ChangeListItemEventContext.getFileModel();
        if (modifyModel != null) {
            MessageBox box = MessageBox.prompt("修改名称", "");
            box.getTextBox().setValue(modifyModel.getName());
            final Listener<WindowEvent> listener = new Listener<WindowEvent>() {

                public void handleEvent(WindowEvent ce) {
                    Dialog dialog = (Dialog) ce.component;
                    TextField<String> text = (TextField<String>) dialog.getFocusWidget();
                    String userData = (String) text.getValue();
                    Button btn = dialog.getButtonPressed();
                    if (btn.getText().equals("Ok")) {
                        if (userData != null) {
                            if (!modifyHandleFile.handleModifyName(userData)) {
                                MessageBox.alert("提示", "已经存在" + userData + "，不能修改", null);
                            }
                        }
                    }

                }
            };
            box.addCallback(listener);
        }
    }

    protected void deleteFolderEvent() {
        final FileModel fileModel = ChangeListItemEventContext.getFileModel();
        final Listener<WindowEvent> listener = new Listener<WindowEvent>() {

            public void handleEvent(WindowEvent ce) {
                Dialog dialog = (Dialog) ce.component;
                Button btn = dialog.getButtonPressed();
                if (btn.getText().equals("Yes")) {
                    Info.display("MessageBox", "确认删除目录:{0}", fileModel.getName());
                }

            }
        };

        if (fileModel != null) {
            MessageBox.confirm("提示", "是否确认删除 " + fileModel.getName() + "<br>该操作将会删除目录下所有子文件", listener);
        }
    }
}
